import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';
import { setLimitDepth } from './Board';
const { ccclass, property } = _decorator;

@ccclass('Xiangqi_DifficultyPanel')
export default class Xiangqi_DifficultyPanel extends Component {
    static Instance: Xiangqi_DifficultyPanel;

    @property(Node)
    Node_难度: Node | null = null;

    buttons = [];
    难度_index = 0;
    onLoad() {
        Xiangqi_DifficultyPanel.Instance = this;

        this.addListeners();
        this.updateButtonHighlights();
    }

    addListeners() {
        const buttons = this.Node_难度.children;
        for (let index = 0; index < buttons.length; index++) {
            const button = buttons[index];
            button.on(Node.EventType.TOUCH_END, () => {
                this.难度_index = index;
                this.On_Button_难度();
            }, this);
        }
    }

    On_Button_难度() {
        console.log('On_Button_难度', this.难度_index);

        let depth = this.难度_index + 1;
        depth = Math.pow(2, depth);
        console.log("depth==", this.难度_index, depth);
        setLimitDepth(depth);

        this.updateButtonHighlights();

        this.playButtonAudio();
    }

    //    // 更新按钮高亮状态
    updateButtonHighlights() {
        // 重置所有按钮的高亮状态
        this.resetButtonHighlights();

        let node = this.Node_难度.children[this.难度_index];
        this.highlightButton(node);
    }

    //    // 重置所有按钮的高亮状态
    resetButtonHighlights() {
        const buttons = this.Node_难度.children;
        for (let index = 0; index < buttons.length; index++) {
            const button = buttons[index];
            this.resetButtonHighlight(button);
        }
    }
    //    // 高亮指定按钮
    highlightButton(button: Node) {
        if (!button) return;

        // 只改变按钮颜色为高亮色
        // button.color = new cc.Color(255, 215, 0, 255); // 金黄色高亮
        button.getComponent(Sprite).color = new Color().fromHEX(`#73CB39`); // 选中高亮色

        let Label_text = button.getComponentInChildren(Label);
        if (Label_text) {
            Label_text.color = new Color().fromHEX(`#FFF9C4`);
        }
    }
    //    // 重置按钮高亮状态
    resetButtonHighlight(button: Node) {
        if (!button) return;

        // 恢复默认颜色
        // button.color = Color.WHITE; // 默认颜色
        button.getComponent(Sprite).color = new Color().fromHEX(`#525963`); // 默认颜色

        let Label_text = button.getComponentInChildren(Label);
        if (Label_text) {
            Label_text.color = new Color().fromHEX(`#93A7BE`);
        }
    }

    playButtonAudio() {
        // 播放按钮音效
        // const AudioManager = require("../../../Script/Manager/AudioManager").default;
        // if (AudioManager) {
        //     AudioManager.PlayButtonAudio();
        // }
    }
}